<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/**
 * CRON to create the moving average table (for non boolean modules): 15 minute MA
 * @author Duleepa Wijayawardhana <dups@bioware.com>
 * @version Thu Oct 19 11:59:00 2006
 */
require("/bigdaddy/phplib/init/cron_init.inc");
set_time_limit(0);
define("CRON_ID", 198);
cronstart(CRON_ID);

$sql = "SELECT username, cn, firstname, lastname, active
    FROM users";
$aResult = db_fetch_array(db_query($sql, array("dbHandle" => "internal")));

if (is_array($aResult))
{
    foreach ($aResult as $aRow)
    {
        $sql = "SELECT COUNT(*) as count FROM users WHERE username = '".$aRow["username"]."'";
        $aCount = db_fetch_result(db_query($sql));
        
        if ($aRow["firstname"] == "")
        {
            preg_match("/(.*?) (.*)/", $aRow["cn"], $aMatches);
            $aRow["firstname"] = $aMatches[1];
            $aRow["lastname"] = $aMatches[2];
        }
        
        if ($aCount["count"] == 0)
        {
            $sql = "INSERT INTO users SET
                username = '".$aRow["username"]."',
                cn = '".db_escape_string($aRow["cn"])."',
                fullname = '".db_escape_string($aRow["cn"])."',
                firstname = '".db_escape_string($aRow["firstname"])."',
                lastname = '".db_escape_string($aRow["lastname"])."',
                active = '".$aRow["active"]."'";
            //echo $sql;
            db_query($sql);
        }
        else
        {
            $sql = "UPDATE users SET
                cn = '".db_escape_string($aRow["cn"])."',
                fullname = '".db_escape_string($aRow["cn"])."',
                firstname = '".db_escape_string($aRow["firstname"])."',
                lastname = '".db_escape_string($aRow["lastname"])."',
                active = '".$aRow["active"]."'
                WHERE username = '".$aRow["username"]."'";
            //echo $sql;
            db_query($sql);
        }
    }
}

cronend(CRON_ID);

?>
